package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 三石
 * Date: 2022-05-21
 * Time: 18:41
 */
public class DelOperation implements IOoperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner scanner=new Scanner(System.in);

        System.out.println("请输入要删除的书籍：");
        String name=scanner.nextLine();

        int currentSize=bookList.getUsedSize();
        int index=-1;

        for (int i = 0; i <currentSize; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                index=i;
                break;
            }
        }
        for (int j = index; j < currentSize; j++) {
            Book book=bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setBooks(currentSize-1,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功！");
    }
}
